import {
	ref
} from 'vue';
/*
  @param filePath 本地待上传的文件路径
  @param bucketFilePath 远程云存储桶的路径
  @param callback 上传成功或者失败的回调
  */
export const uploadFileToOSS = (filePath, bucketFilePath, callback) => {
	const apiUrl = 'https://sansancm.ddddcc.cc/digest/generate_signature';
	return wx.request({
		url: apiUrl,
		success: (res) => {
			const formData = {
				key: bucketFilePath,
				policy: res.data.policy,
				'x-oss-signature-version': res.data.x_oss_signature_version,
				'x-oss-credential': res.data.x_oss_credential,
				'x-oss-date': res.data.x_oss_date,
				'x-oss-signature': res.data.signature,
				'x-oss-security-token': res.data.security_token,
				success_action_status: '200',
			};
			wx.uploadFile({
				url: VITE_OSS_URL,
				filePath: filePath,
				name: 'file',
				formData: formData,
				success(res) {
					// console.log('上传响应:', res);
					if (res.statusCode === 200) {
						callback(null, res.data);
					} else {
						// console.error('上传失败，状态码:', res.statusCode);
						// console.error('失败响应:', res);
						callback(res);
					}
				},
				fail(err) {
					wx.showToast({
						title: '上传失败，请重试!',
						icon: 'none',
					});
					callback(err);
				},
			});
		},
		fail: (err) => {
			// console.error('请求接口失败:', err);
			wx.showToast({
				title: '获取上传参数失败，请重试!',
				icon: 'none',
			});
		},
	});
};